﻿<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<html>
	<!--
	页面接收参数：	 expression : 表达式内容
	 			 expressionType: 表达式类型 1 条件表达式，2 运算表达式
	 			 cateType：变量分类 ,对应variable_category的cate_type字段
	 -->
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<link type="text/css" rel="stylesheet" href="themes/style.css" />
	<script language="javascript" src='../../dwr/interface/formularEditorManager.js'></script>
	<script language="javascript" src="../../dwr/engine.js"></script>
	<script language="javascript" src="../../dwr/util.js"></script>
	<script type="text/javascript" src="./fill2cursor.js"></script>
	<script language="javascript">
	function onException(message,e){
		alert("出错了: " + message);
	}
	DWREngine.setErrorHandler(onException);
	//标识表达式是否合法
	var isValidExpression = false;
	/**
		页面初始化
	*/
	function initPage(){
		//初始化变量分类列表
		formularEditorManager.loadVariableCategoryList('${param.cateType}',
			function(result){
				DWRUtil.addOptions('varCatagorySelect', result,'cateId', 'cateName'); 
			}
		);
		//填充表达式的值
		if(dialogArguments && dialogArguments.expression){
			document.getElementById("expression").value = dialogArguments.expression;
		}
		//初始化运算符（如果是运算表达式,则比较运算符无效）
		if('1' == '${param.expressionType}'){
			var nodes = document.getElementsByName('__compareOperator');
			for(var i = 0 ; i < nodes.length;i++){
				nodes[i].disabled = true;
			}
		}
		//设置textArea的焦点
		cursPos = null;
		//单独打开编辑框时让表达式处于可编辑状态
		if(!document.getElementById("expressionTable")){
			document.getElementById("expression").disabled = false;
		}
	}
	/*处理变量分类改变*/
	function processVarCatagoryChanaged(varCatagory){
		if(""!=varCatagory){
			formularEditorManager.loadVariableList(varCatagory,'${param.cateType}',
				function(result){
					DWRUtil.removeAllOptions('dataItemsSelect');
					DWRUtil.addOptions('dataItemsSelect', result,'itemCode', 'itemName'); 
				}
			);
		}
		DWRUtil.removeAllOptions('dataItemValuesSelect');
	}
	
	/*加载数据项对应的值集*/
	function loadDataItemValues(ele){
		if(typeof(ele.value) !="undefined" &&  ele.value != null && "" != ele.value){
			var itemId = ele.value;
			if(""!=itemId){//加载数据项对应的值集
				formularEditorManager.loadValuesOfDataItem(itemId,
					function(result){
						DWRUtil.removeAllOptions('dataItemValuesSelect');
						DWRUtil.addOptions('dataItemValuesSelect', result,'DATA_TYPE', 'TEXT'); 
					}
				);
			}
		}
		DWRUtil.removeAllOptions('dataItemValuesSelect');
	}
	//初始化运算符的可用状态和
	function initOperators(){
		
	}
	//表达式保存
	function doSave(btn){
		if(!isValidExpression){
			alert('表达式未经验证或表达式不合法,请检查');
		}
		btn.disabled = true;
		var exprEle = document.getElementById('expression');
		var expression = exprEle.value;
		if(exprEle.cellId){//包含列表
			//将表达式内容保存至列表
			document.getElementById(exprEle.cellId).innerHTML= expression;
			exprEle.disabled="true";
			document.getElementById("promptInfo").innerHTML = "请点击要编辑的表达式所在单元格进行编辑";
		}else{
			window.returnValue = expression;
			window.close();
		}
	}
	
	//表达式验证
	function doValidate(){
		var expression = document.getElementById('expression').value;
		if("" == expression.replace(/&nbsp;/g," ").replace(/(^[ \t]*)|([ \t]*$)/g,"")){//表达式为空不用验证
			alert("表达式内容为空,请检查");
			return;
		}
		//获取表达式类型
		var expressionType = '${param.expressionType}';
		if(expressionType ==''){
			expressionType = document.getElementById('expression').expressionType;
		}
		formularEditorManager.validateExpression(expression,expressionType,
				function(result){
					if(result){
						if(result.pass){//表达式验证通过
							alert("验证成功，您可以点击保存按钮进行保存");
							isValidExpression = true;
							document.getElementById('saveButton').disabled = false;
						}else{
							alert("表达式验证失败 : "+result.message);
						}
					}
				}
			);
	}
	//处理表达式改变
	function processExpressionChange(value){
			isValidExpression = false;
			document.getElementById('saveButton').disabled = 'disabled';
	}
	/*插入选中的变量值*/
	function processDataItemSelected(dataitemSelectElement){
		if(dataitemSelectElement.value && dataitemSelectElement.vlaue != ""){
			var dataType = dataitemSelectElement.value;
			var value = dataitemSelectElement.options[dataitemSelectElement.selectedIndex].innerText;
			if("string" == dataType || "date"==dataType){
				value = "'" + value +"'";
			}
			fill2cursor(value,'expression');
		}
		
	} 
	//处理选中变量
	function processVariableSelected(varEle){
		if(varEle.value && varEle.vlaue != ""){
			fill2cursor(varEle.options[varEle.selectedIndex].innerText,'expression');
		}
	}
	</script>
	<head>
		<title>表达式定义</title>

	</head>
	<body id="opinionform" onload="initPage();">
		<div id="container2">
			<div id="content">
				<div class="topform"
					style="margin-left: 0px; margin-top: 0px; width: 755px">
					<h2>
						表达式内容
					</h2>
					<textarea rows="4" id="expression" disabled="disabled"
						onmouseup="TraceCursorPosition(this)"
						onmousedown="TraceCursorPosition(this)"
						onkeyup="TraceCursorPosition(this)"
						onkeydown="TraceCursorPosition(this)"
						onfocus="TraceCursorPosition(this)"
						onchange="processExpressionChange(this.value)">${param.expression}</textarea>
				</div>
				<div class="leftform" style="margin-left: 0px" style="width:220px">
					<h2>
						请选择定义条件分类
					</h2>
					<select style="width: 100%" id="varCatagorySelect"
						onchange="processVarCatagoryChanaged(this.value)">
						<option value="">
							请选择
						</option>
					</select>
					<!-- 数据项内容 -->
					<select size="9" style="width: 100%" id="dataItemsSelect"
						onclick="loadDataItemValues(this);"
						ondblclick="processVariableSelected(this);">
					</select>
				</div>
				
				<div class="middleform" style="width: 223px; height: 190px;">
					<h2 style="height: 23px">
						选择对应数据项的值
					</h2>
					<select size="11" style="width:100%;height: 180px" id="dataItemValuesSelect" 
						ondblclick="processDataItemSelected(this);">
					</select>
				</div>
				<div class="rightform" style="width: 314px; height: 240px;margin: 0px">
					<h2>
						运算符号
					</h2>
					<div class="toprightform" id="operatorDiv" style="padding-left: 5px;padding-top: 5px;padding-right: 2px;padding-bottom: 5px;width: 100%;height: 100%;vertical-align: middle;">
						<input type="button" value="加" class="btn test" style="margin-bottom: 2px"
							onclick="fill2cursor('+','expression');" />
						<input type="button" value="减" class="btn test"
							onclick="fill2cursor('-','expression');" />
						<input type="button" value="乘" class="btn test"
							onclick="fill2cursor('*','expression');" />
						<input type="button" value="除" class="btn test"
							onclick="fill2cursor('/','expression');" />
						<input type="button" value="IsNull" class="btn test" onclick="fill2cursor('IsNull(,0)','expression');"/>
						<input type="button" value="Round" class="btn test2" onclick="fill2cursor('Round(,2)','expression');"/>
						<br />
						<input type="button" value="=" class="btn test" name="__compareOperator" style="margin-bottom: 2px"
							onclick="fill2cursor(this.value,'expression');" />
						<input type="button" value="&lt;" class="btn test" name="__compareOperator"
							onclick="fill2cursor('<','expression');" />
						<input type="button" value="&gt;" class="btn test" name="__compareOperator"
							onclick="fill2cursor('>','expression');" />
						<input type="button" value="&lt;&gt;" class="btn test" name="__compareOperator"
							onclick="fill2cursor('<>','expression');" />
						<input type="button" value="&gt;=" class="btn test" name="__compareOperator"
							onclick="fill2cursor('>=','expression');" />
						<input type="button" value="&le;" class="btn test" name="__compareOperator"
							onclick="fill2cursor('<=','expression');" /> 
						<input type="button" value="Like" class="btn test" name="__compareOperator"
							onclick="fill2cursor(this.value,'expression');" />
						<br />
						<input type="button" value="And" class="btn test" name="__compareOperator" style="margin-bottom: 2px"
							onclick="fill2cursor(this.value,'expression');" />
						<input type="button" value="Or" class="btn test" name="__compareOperator"
							onclick="fill2cursor(this.value,'expression');" />
						<input type="button" value="Not" class="btn test" name="__compareOperator"
							onclick="fill2cursor(this.value,'expression');" />
						<input type="button" value="(" class="btn test"
							onclick="fill2cursor(this.value,'expression');" />
						<input type="button" value=")" class="btn test"
							onclick="fill2cursor(this.value,'expression');" />
						<input type="button" value="清空" class="btn test2"
							onclick="document.getElementById('expression').value='';" />
						<br />
						
						<input type="button" value="验证条件表达式" class="btn test3" style="margin-bottom: 2px"
							onclick="doValidate();" />
						<input type="button" value="保存" class="btn test2"
							onclick="doSave(this);" id="saveButton" disabled="disabled"
							title="表达式验证之后才可进行保存" />
						<input type="button" value="退出" class="btn test2"
							onclick="window.close();" />
					</div>
				</div>
				
			</div>
	</body>
</html>
